package com.hm.service.request.test;

import com.hm.service.http.HttpUtils;
import com.hm.service.utils.EncryptionUtils;
import com.hm.service.utils.JsonUtils;
import okhttp3.*;

import java.io.File;
import java.io.IOException;


/**
 * 获取session接口
 */
public class Request05 {

    private static final String url = "http://172.26.2.18:9001/SZCG/MAIN/UPLOAD_PICTURE_001";

    //秘钥
    private static final String secretKey = "kokL8G4RmNfV69cdcJ9dfnrlFGsLy6mH";

    public static void main(String[] args) {

        File file = new File("D:/SZCG/桌面/图片服务/0001/1111.pdf");

        // 创建RequestBody
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/jpeg"), file)).build();

        // 创建Request
        Request request = new Request.Builder()//
                .addHeader("permit", "123456789")//
                .url(url)//
                .post(requestBody)//
                .build();//

        try {
            // 执行请求
            OkHttpClient okHttpClient = HttpUtils.getOkHttpClient();
            Response response = okHttpClient.newCall(request).execute();
            ResponseBody resBody = response.body();
            int code = response.code();
            if (code == 200 && resBody != null) {
                String jsonString = resBody.string();
                boolean json = JsonUtils.isJson(jsonString);
                System.out.println("ResponseBody-->:" + json);
                System.out.println(json ? jsonString : EncryptionUtils.decrypt(secretKey, jsonString));
            } else {
                System.out.println("ResponseBody为空:");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
